package com.kung.qvs.ui.main.fragment.manage;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;

import com.kung.qvs.BR;
import com.kung.qvs.R;
import com.kung.qvs.app.AppViewModelFactory;
import com.kung.qvs.databinding.FragProductEditBinding;
import com.kung.qvs.entity.ProductBean;
import com.kung.qvs.ui.main.KungViewModel;
import com.kung.qvs.ui.widget.ConformDialog;

import me.goldze.mvvmhabit.base.BaseFragment;

public class FragProductAdd extends BaseFragment<FragProductEditBinding, FragProductAddModel> {
    private KungViewModel kungViewModel;
    private ProductBean mProductBean;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_product_edit;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public FragProductAddModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(getActivity(), factory).get(FragProductAddModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        if (getArguments() != null) {
            mProductBean = (ProductBean) getArguments().getSerializable("product");
        }
    }

    @Override
    public void initData() {
        super.initData();
        kungViewModel = new ViewModelProvider(getActivity()).get(KungViewModel.class);
        if (mProductBean != null) {
            viewModel.mIsUpdate.set(true);
            viewModel.mProductId.set(mProductBean.getProductId());
            viewModel.mProductName.set(mProductBean.getProductName());
            viewModel.mParentId.set(mProductBean.getParentId());
            viewModel.mProductWeight.set(mProductBean.getWeight());
            viewModel.mConvertValue.set(mProductBean.getUnit());
            viewModel.mProductRemark.set(mProductBean.getRemark());
        } else {
            viewModel.mIsUpdate.set(false);
            viewModel.mProductId.set("");
            viewModel.mProductName.set("");
            viewModel.mParentId.set("");
            viewModel.mProductWeight.set("");
            viewModel.mConvertValue.set("");
            viewModel.mProductRemark.set("");
        }
        int select = viewModel.funLoadProductTypes(kungViewModel.getmProductTypes().getValue());
        //延时200毫秒，等待数据加载完成
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.spinnerProductType.setSelection(select, true);
            }
        }, 200);

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        //返回上个页面
        viewModel.uc.eventToLastPage.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                //关闭页面，返回首页
                Navigation.findNavController(getView()).navigateUp();
            }
        });

        //保存产品
        viewModel.uc.eventSaveProduct.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                ConformDialog.Builder builder = new ConformDialog.Builder(getActivity());
                builder.setMessage("是否保存该产品？", "");
                builder.setTitle("保存产品");
                builder.setPositiveButton("确认",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog2,
                                                int which) {
                                dialog2.dismiss();
                                viewModel.funSaveProduct();
                            }
                        });

                builder.setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog2,
                                                int which) {
                                dialog2.dismiss();
                            }
                        });
                builder.create().show();
            }
        });
    }
}
